import http from './http'
import type { SiteInfo } from '@/types/siteInfo'
import { apiMsgProp } from '@/utils/global'

const SiteInfoService = {
    // 新增FQA、公告、联系我们
    addAction: async (data: object): Promise<SiteInfo[]> => {
        const res = await http.post(`/course/site-info/add`, data)
        apiMsgProp(res.data.code, res.data.message)
        if (res.data.code !== 200) return Promise.reject(res.data)

        return res.data.data
    },
    // 获取FAQ列表
    FAQ: async (): Promise<SiteInfo[]> => {
        const res = await http.get('/course/site-info/faq/list')
        if (res.data.code !== 200) {
            apiMsgProp(res.data.code, res.data.message)
            return Promise.reject(res.data)
        }

        return res.data.data.items
    },
    // 获取公告列表
    announcement: async (): Promise<SiteInfo[]> => {
        const res = await http.get('/course/site-info/announcement/list')
        if (res.data.code !== 200) {
            apiMsgProp(res.data.code, res.data.message)
            return Promise.reject(res.data)
        }

        return res.data.data.items
    },
    // 获取联系我们列表
    contact: async (): Promise<SiteInfo[]> => {
        const res = await http.get('/course/site-info/contact/list')
        if (res.data.code !== 200) {
            apiMsgProp(res.data.code, res.data.message)
            return Promise.reject(res.data)
        }

        return res.data.data.items
    },
    // 查看单个信息明细
    info: async (id: number): Promise<SiteInfo> => {
        const res = await http.get(`/course/site-info/${id}`)
        if (res.data.code !== 200) {
            apiMsgProp(res.data.code, res.data.message)
            return Promise.reject(res.data)
        }

        return res.data.data.items
    },
    // 删除
    deleteAction: async (id: number): Promise<void> => {
        const res = await http.delete(`/course/site-info/${id}`)
        apiMsgProp(res.data.code, res.data.message)
        if (res.data.code !== 200) return Promise.reject(res.data)
    },
    // 编辑FQA、公告、联系我们
    editAction: async (id: number, data: object): Promise<SiteInfo[]> => {
        const res = await http.put(`/course/site-info/${id}`, data)
        apiMsgProp(res.data.code, res.data.message)
        if (res.data.code !== 200) return Promise.reject(res.data)

        return res.data.data
    },
}

export default SiteInfoService
